import { Component, OnInit } from '@angular/core';
import { CommonService } from '../../../../services/common.service';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-contractextractioneffect',
  templateUrl: './contractextractioneffect.component.html',
  styleUrls: ['./contractextractioneffect.component.css']
})



export class ContractextractioneffectComponent implements OnInit {
  fileName = "";
  public noData: boolean = false;
  public uploadFileName: string = ""
  public isCanTest: boolean = false; //是否可以点击测试，防止文件过大，在转化的过程中客户点击测试按钮
  public file: any = { file_name: "", file_base64: null };
  public queryJobTimer: any = null; //轮询定时器
  public resultTable: any[] = []

  changeFile(a: string) {
    this.fileName = a
  }
  constructor(public http: CommonService,
    private message: NzMessageService) {

  }

  ngOnInit(): void {
  }

  ngOnDestroy(): void {
    //Called once, before the instance is destroyed.
    //Add 'implements OnDestroy' to the class.
    if (this.queryJobTimer) {
      clearInterval(this.queryJobTimer)
    }
  }
  // 测试该项目的合同抽取效果（异步）
  testProject() {
    if (this.uploadFileName == "") {
      return;
    }

    if (!this.isCanTest) {
      this.message.info('文件正在上传，请稍等！');
      return;
    }
    let that = this;
    // 项目id
    let id = sessionStorage.getItem('id');
    this.http.post('/contract-extraction/evaluation/test-project', {
      project_id: id,
      file_name: that.file.file_name,
      file_data_base64: that.file.file_base64
    }).then((res: any) => {
      if (res.error_code == "AIS.0000") {
        that.file.job_id = res.result.job_id
        that.queryJob();
        that.message.success('测试文件成功，请等待测试结果！');
      }
    }).catch((err: any) => {
      that.message.error(err.error_msg)
    })
  }
  changeNodeta() {
    this.noData = !this.noData;
  }
  getFile(e) {
    let that = this;
    let files = e.target.files[0];
    let fileName = files.name;
    var reader = new FileReader();
    reader.readAsDataURL(files);
    reader.onload = function () {
      that.file.file_name = fileName;
      let file64 = reader.result.toString();
      that.file.file_base64 = file64.substring(file64.indexOf(',') + 1);
      that.isCanTest = true;
      that.uploadFileName = fileName;
    };
  }

  queryJob() {
    let that = this;
    this.queryJobTimer = setInterval(function () {
      var param = {
        job_id: that.file.job_id
      }
      that.http.post('/contract-extraction/job-mgr/query-job', param).then((res: any) => {
        if (res.error_code == "AIS.0000") {
          if (res.result.job_status == "success") {
            that.resultTable = res.result.job_result;
            that.message.success("获取测试结果成功！");
            clearInterval(that.queryJobTimer);
          } else if (res.result.job_status == "fail" || res.result.job_status == "timeout") {
            that.message.success("获取测试结果失败！");
            clearInterval(that.queryJobTimer);
          }
        } else {
          that.message.error(res.error_msg); clearInterval(that.queryJobTimer);
        }
      }).catch((err: any) => {
        that.message.error(err.error_msg); clearInterval(that.queryJobTimer);
      });
    }, 1000)

  }
}
